  Page({
    data: {
      campuses: [] as string[], // 使用空数组初始化
      selectedCampus: '',
      restaurants: [] as string[], // 使用空数组初始化
      floors: [] as string[], // 使用空数组初始化
      selectedRestaurant: '',
      selectedFloor: '',
      stall: '',
      dish: '',
      price: '',
      imageBase64: '' as string | undefined,
      campusData: {} as CampusData // 添加 campusData 属性
    },
  
    onLoad: function() {
      this.fetchData();
    },
  
    fetchData: function() {
      const that = this;
      wx.request({
        url: 'https://food.api.lttac.cn/api/getData', // 更新为你的后端接口
        method: 'GET',
        success: function(res) {
          if (typeof res.data === 'object' && res.data !== null && 'campuses' in res.data) {
            const campusData: CampusData = {};
            const campuses = res.data.campuses.map((campus: any) => {
              campusData[campus.name] = {};
              campus.restaurants.forEach((restaurant: any) => {
                campusData[campus.name][restaurant.name] = restaurant.floors.map((floor: any) => floor.name);
              });
              return campus.name;
            });
            that.setData({
              campuses: campuses, // 使用从接口获取的校区数据
              campusData: campusData,
              restaurants: [],
              floors: []
            });
          } else {
            console.error('Invalid response format', res.data);
          }
        },
        fail: function(error) {
          console.error('Request failed', error);
        }
      });
    },
  
    bindCampusChange: function(e: any) {
      const selectedCampus = this.data.campuses[e.detail.value];
      const restaurants = Object.keys(this.data.campusData[selectedCampus] || {});
      this.setData({
        selectedCampus: selectedCampus,
        restaurants: restaurants as string[],
        floors: [],
        selectedRestaurant: '',
        selectedFloor: '',
        stall: '',
        dish: '',
        price: '',
        imageBase64: ''
      });
    },
  
    bindRestaurantChange: function(e: any) {
      const selectedRestaurant = this.data.restaurants[e.detail.value];
      const floors = selectedRestaurant === '' ? [] : this.data.campusData[this.data.selectedCampus][selectedRestaurant] as unknown as string[];
      this.setData({
        selectedRestaurant: selectedRestaurant,
        floors: floors,
        selectedFloor: '',
        stall: '',
        dish: '',
        price: '',
        imageBase64: ''
      });
    },
  
    bindFloorChange: function(e: any) {
      const selectedFloor = this.data.floors[e.detail.value];
      this.setData({
        selectedFloor: selectedFloor,
        stall: '',
        dish: '',
        price: '',
        imageBase64: ''
      });
    },
  
    handleInputChange: function(e: any) {
      const { field } = e.currentTarget.dataset;
      this.setData({
        [field]: e.detail.value
      });
    },
  
    chooseImage: function() {
        const that = this;
        wx.chooseMedia({
          count: 1,
          mediaType: ['image'],
          sourceType: ['album', 'camera'],
          success: function(res) {
            const tempFilePath = res.tempFiles[0].tempFilePath;
            wx.getFileSystemManager().readFile({
              filePath: tempFilePath,
              encoding: 'base64',
              success: function(fileRes) {
                that.setData({
                  imageBase64: fileRes.data as string
                });
              },
              fail: function(error) {
                console.error('读取文件失败', error);
              }
            });
          },
          fail: function(error) {
            console.error('选择图片失败', error);
          }
        });
      },
  
    handleSubmit: function() {
      const { selectedCampus, selectedRestaurant, selectedFloor, stall, dish, price, imageBase64 } = this.data;
  
      if (!selectedCampus || !selectedRestaurant || !selectedFloor || !stall || !dish || !price) {
        wx.showToast({
          title: '请填写所有字段',
          icon: 'none'
        });
        return;
      }
  
      const that = this;
      const requestData: any = {
        campus: selectedCampus,
        restaurant: selectedRestaurant,
        floor: selectedFloor,
        stall: stall,
        dish: dish,
        price: price
      };
  
      if (imageBase64) {
        requestData.image = imageBase64;
      }
      wx.request({
        url: 'https://food.api.lttac.cn/api/addData', // 你的后端接口
        method: 'POST',
        data: requestData,
        success: function(res: { data: { success: boolean } }) {
          if (res.data.success) {
            wx.showToast({
              title: '数据添加成功',
              icon: 'success'
            });
            that.setData({
              stall: '',
              dish: '',
              price: '',
              imageBase64: ''
            });
          } else {
            wx.showToast({
              title: '数据添加失败',
              icon: 'none'
            });
          }
        },
        fail: function() {
          wx.showToast({
            title: '请求失败，请重试',
            icon: 'none'
          });
        }
      });
    }
  });
  